package no1375;

public class No1375 {

    public static void main(String[] args) {
        int[] flips = {2, 1, 3, 5, 4};
        int count = new No1375().numTimesAllBlue(flips);
        int count1 = new No1375().numTimesAllBlue1(flips);
        int count2 = new No1375().numTimesAllBlue2(flips);
        System.out.println(count);
        System.out.println(count1);
        System.out.println(count2);
    }

    // 这题关键是读懂题目含义，
    // 让所有开着的灯都变成蓝色的时刻的性质是：此时点亮最远的灯的位置恰巧等于点亮灯的个数。
    // 若点亮最远的灯的位置大于此时点亮灯的个数，则意味着在点亮最远的灯的位置之前存在着未点亮的灯。
    private int numTimesAllBlue2(int[] flips) {
        int size = flips.length;
        int count = 0, maxReachingPoint = 0;
        for (int i = 0 ; i < size; i++){
            // 点灯的位置 更远的方向有没有亮的灯
            maxReachingPoint = Math.max(maxReachingPoint, flips[i]);
            if ( i + 1 == maxReachingPoint){
                count += 1;
            }
        }
        return count;
    }

    private int numTimesAllBlue1(int[] flips) {
        int count = 0;
        int[] ss = new int[flips.length];
        for (int i = 0; i < flips.length; i++) {
            boolean isBlue = true;
            ss[flips[i] - 1] = 1;
            if(flips[i] <= i+1){
                for (int j = 0; j <= i; j++) {
                    if (ss[j] != 1) {
                        isBlue = false;
                        break;
                    }
                }
                if (isBlue)
                    count++;
            }
        }
        return count;
    }

    public int numTimesAllBlue(int[] flips) {
        int count = 0;
        for (int i = 0; i < flips.length; i++) {
            boolean isBlue = true;
            if (flips[i] <= i + 1) {
                for (int j = i + 1; j < flips.length; j++) {
                    if (flips[j] <= i + 1) {
                        isBlue = false;
                        break;
                    }
                }
                if (isBlue)
                    count++;
            }

        }
        return count;
    }
}
